package com.mediacloud.app.newsmodule.activity;


import android.content.Context;
import android.os.Bundle;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.mediacloud.app.assembly.util.Utility;
import com.mediacloud.app.model.adaptor.BaseAdaptor;
import com.mediacloud.app.newsmodule.R;

public class AudioVodRelativeBottomFragment<T> extends BottomSheetDialog
{
    public static final int StreamMode=1;
    public static final int AudioListMode=2;

    public int mode=StreamMode;
    protected View contentView;
    protected Context mContext;
    protected AdapterView.OnItemClickListener listener;
    protected BaseAdaptor<T> adaptor;
    public ListView audio_bottom_list;
    public AudioVodRelativeBottomFragment(@NonNull Context context)
    {
        super(context);
        mContext=context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView= LayoutInflater.from(mContext).inflate(R.layout.aappfactory_aduio_streamsheet, null);
        setContentView(contentView);
        contentView.findViewById(R.id.frame_title).setVisibility(showTitle ? View.VISIBLE : View.GONE);
        /*contentView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                boolean b= contentView.onTouchEvent(event);
                dismiss();
                return b;
            }
        });*/
        audio_bottom_list= Utility.findViewById(contentView, R.id.audio_bottom_list);
//		audio_bottom_list.setPullLoadEnable(false);
//		audio_bottom_list.setPullRefreshEnable(false);
        audio_bottom_list.setHeaderDividersEnabled(false);
        audio_bottom_list.setDivider(null);
        audio_bottom_list.setFooterDividersEnabled(false);
        audio_bottom_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                dismiss();
                if(listener!=null)
                {
                    listener.onItemClick(parent, view, position, id);
                }
            }
        });
        contentView.setMinimumWidth(mContext.getResources().getDisplayMetrics().widthPixels);
        audio_bottom_list.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
              audio_bottom_list.getViewTreeObserver().removeOnPreDrawListener(this);
               /*   audio_bottom_list.getLayoutParams().height = (int) (mContext.getResources().getDisplayMetrics().heightPixels / 2.5);
                audio_bottom_list.setMinimumHeight(audio_bottom_list.getLayoutParams().height);*/
                audio_bottom_list.setMinimumWidth(mContext.getResources().getDisplayMetrics().widthPixels);
                audio_bottom_list.requestLayout();
                return true;
            }
        });
        ImageView imageViewClose = Utility.findViewById(contentView, R.id.image_close);
        imageViewClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setItemClickListener(AdapterView.OnItemClickListener listener)
    {
        this.listener=listener;
    }

    public void setListAdaptor(BaseAdaptor<T> adaptor)
    {
        this.adaptor=adaptor;
        audio_bottom_list.setAdapter(this.adaptor);
    }
    public void show(final View view)
    {
        dismiss();
        int heightPixels=mContext.getResources().getDisplayMetrics().heightPixels;
        /*if(contentView.getHeight()<=0)
        {
            contentView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                public boolean onPreDraw() {
                    contentView.getViewTreeObserver().removeOnPreDrawListener(this);
                    dismiss();
                    int heightPixels=mContext.getResources().getDisplayMetrics().heightPixels;
                    showAtLocation(view, Gravity.NO_GRAVITY,0, heightPixels-contentView.getHeight());
                    return true;
                }
            });
        }
        showAtLocation(view, Gravity.NO_GRAVITY,0, heightPixels-contentView.getHeight());*/
        show();
    }

    public void setListviewBackgroundColor(@ColorInt int color){
        if(audio_bottom_list != null)
        audio_bottom_list.setBackgroundColor(color);
    }

    boolean showTitle;
    public void show(View view, boolean showTitle) {
        this.showTitle=showTitle;
        show();
    }
}